package flow.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/timer")
public class TimerController {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    ObjectMapper objectMapper;

    //启动流程并添加流程变量
    @PostMapping("/start")
    public String startProcess(@RequestBody String username) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", username);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("timerEvent", map);
        return "提交成功.流程Id为：" + processInstance.getId();
    }

    //查询任务及流程变量
    @GetMapping("/task")
    public String listTaskDetail(@RequestParam String assignee) {
        List<Task> taskList = taskService.createTaskQuery()
//                .taskAssignee("manager")
                .orderByTaskCreateTime()
                .desc()
                .list();
        for (Task task : taskList) {
            Object username = taskService.getVariables(task.getId());
            Map<String, Object> variables = runtimeService.getVariables(task.getExecutionId());
        }
        return taskList.toString();
    }

    //完成任务并添加流程变量
    @PostMapping("/task")
    public void completeTask(@RequestBody String taskId) {
        Task task = taskService.createTaskQuery()
                .orderByTaskCreateTime()
                .desc()
                .singleResult();
        Map<String, Object> variables = new HashMap<>();
        variables.put("complete", task);
        taskService.complete(task.getId(), variables);
    }

    //查询历史任务及历史流程变量
    @GetMapping("/task/history")
    public void listTaskHistory() {
        List<HistoricTaskInstance> taskList = historyService.createHistoricTaskInstanceQuery()
                .orderByHistoricTaskInstanceEndTime()
                .desc()
                .list();
        for(HistoricTaskInstance task : taskList) {
            Map<String, Object> processVariables = task.getProcessVariables(); //0
            Map<String, Object> taskLocalVariables = task.getTaskLocalVariables(); //0
            List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
                    .excludeTaskVariables()
                    .processInstanceId(task.getProcessInstanceId())
                    .list();
        }
    }
}
